package com.digitalchina.lxnsy.activity.old;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.adapter.SimilarPesticideAdapter;
import com.digitalchina.lxnsy.api.BusinessPest;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.Constant;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.constant.URL;
import com.digitalchina.lxnsy.entity.PestDetailVO;
import com.digitalchina.lxnsy.entity.PestVO;
import com.digitalchina.lxnsy.entity.PesticideVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.FrescoUtil;
import com.digitalchina.lxnsy.toolkit.GlideImageLoader;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.digitalchina.lxnsy.widget.MyListView;
import com.facebook.drawee.view.SimpleDraweeView;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2017/4/6 15:21
 * 修改备注：
 * ================================================
 */
public class PestDiagnosisDetailActivity extends BaseActivity {

    @Bind(R.id.diagnosis_detail_tv_name)
    TextView diagnosisDetailTvName; // 病症名
    @Bind(R.id.diagnosis_detail_banner)
    Banner diagnosisDetailBanner;   // 图集
    @Bind(R.id.diagnosis_detail_tv_intro)
    TextView diagnosisDetailTvIntro;    // 简介
    @Bind(R.id.diagnosis_detail_tv_damage)
    TextView diagnosisDetailTvDamage;   // 为害症状
    @Bind(R.id.diagnosis_detail_tv_pathogen)
    TextView diagnosisDetailTvPathogen; // 病原
    @Bind(R.id.diagnosis_detail_tv_cycle)
    TextView diagnosisDetailTvCycle;    // 侵染
    @Bind(R.id.diagnosis_detail_tv_factor)
    TextView diagnosisDetailTvFactor;   // 发生规律
    @Bind(R.id.diagnosis_detail_tv_method)
    TextView diagnosisDetailTvMethod;   // 防治方法
    @Bind(R.id.diagnosis_detail_sdv_head)
    SimpleDraweeView diagnosisDetailSdvHead;    // 专家头像
    @Bind(R.id.diagnosis_detail_tv_expert_name)
    TextView diagnosisDetailTvExpertName;   // 专家姓名
    @Bind(R.id.diagnosis_detail_tv_expert_title)
    TextView diagnosisDetailTvExpertTitle;  // 专家职称
    @Bind(R.id.diagnosis_detail_tv_expert_introduce)
    TextView diagnosisDetailTvExpertIntroduce;  // 专家简介
    @Bind(R.id.diagnosis_detail_iv_chat)
    ImageView diagnosisDetailIvChat;    // 聊天按钮
    @Bind(R.id.diagnosis_detail_layout_expert)
    RelativeLayout diagnosisDetailLayoutExpert; // 专家layout
    @Bind(R.id.diagnosis_detail_lv_pesticide)
    MyListView diagnosisDetailLvPesticide;  // 农药list
    @Bind(R.id.diagnosis_detail_tv_no_expert)
    TextView diagnosisDetailTvNoExpert; // 暂无专家Text
    @Bind(R.id.diagnosis_detail_tv_no_pesticide)
    TextView diagnosisDetailTvNoPesticide;  // 暂无农药Text

    private PestVO pestVO;
    private SimilarPesticideAdapter mAdapter;

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_pest_diagnosis_detail);
        setTitle("病虫害详情");
    }

    @Override
    public void initMember() {
        pestVO = (PestVO) getIntent().getSerializableExtra("pestVO");
        if (pestVO == null)
            return;

        diagnosisDetailTvName.setText(pestVO.getName());


        BusinessPest.getDiagnosisContext(this, pestVO.getId(), mHandler);

    }

    @Override
    public void addListener() {


        diagnosisDetailLvPesticide.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                GotoUtil.gotoActivity(PestDiagnosisDetailActivity.this, PesticideDetailsActivity.class, "pesticideVO", mAdapter.getListData().get(position));
            }
        });

    }

    @Override
    public void setHandler() {

        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);


                switch (msg.what) {
                    case MSG.MSG_GET_DIAGNOSIS_CONTEXT_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_GET_DIAGNOSIS_CONTEXT_FIELD:
                        break;
                }
            }
        };

    }

    @Override
    public void callBack(Object object) {

        PestDetailVO pestDetailVO = FastJsonUtil.getBean(object.toString(), "body", "", PestDetailVO.class);

        if (pestDetailVO == null)
            return;

        setBanner(pestDetailVO.getDisPicList());
        setPestDetail(pestDetailVO.getAgrDisease());
        setExpertList(pestDetailVO.getExpertList());
        setPesList(pestDetailVO.getPesList());
    }

    /**
     * 设置推荐农药
     *
     * @param pesList
     */
    private void setPesList(List<PesticideVO> pesList) {

        if (pesList != null && pesList.size() > 0) {
            diagnosisDetailLvPesticide.setVisibility(View.VISIBLE);
            diagnosisDetailTvNoPesticide.setVisibility(View.GONE);
            mAdapter = new SimilarPesticideAdapter(this, pesList);
            diagnosisDetailLvPesticide.setAdapter(mAdapter);
        } else {
            diagnosisDetailLvPesticide.setVisibility(View.GONE);
            diagnosisDetailTvNoPesticide.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 设置推荐专家
     *
     * @param expertList
     */
    private void setExpertList(List<PestDetailVO.ExpertListBean> expertList) {

        if (expertList != null && expertList.size() > 0) {
            diagnosisDetailLayoutExpert.setVisibility(View.VISIBLE);
            diagnosisDetailTvNoExpert.setVisibility(View.GONE);
            FrescoUtil.showImageSmall(expertList.get(0).getPhoto(), diagnosisDetailSdvHead);
            diagnosisDetailTvExpertName.setText(expertList.get(0).getExpertName());
            diagnosisDetailTvExpertTitle.setText(expertList.get(0).getTitle());
            diagnosisDetailTvExpertIntroduce.setText(expertList.get(0).getExpertIntroduce());
        } else {
            diagnosisDetailLayoutExpert.setVisibility(View.GONE);
            diagnosisDetailTvNoExpert.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 设置病虫害详情
     *
     * @param agrDisease
     */
    private void setPestDetail(PestDetailVO.AgrDiseaseBean agrDisease) {

        diagnosisDetailTvIntro.setText(!StringUtil.isStrEmpty(agrDisease.getIntro()) ? agrDisease.getIntro() : "暂无数据");
        diagnosisDetailTvDamage.setText(!StringUtil.isStrEmpty(agrDisease.getDamage()) ? agrDisease.getDamage() : "暂无数据");
        diagnosisDetailTvPathogen.setText(!StringUtil.isStrEmpty(agrDisease.getPathogen()) ? agrDisease.getPathogen() : "暂无数据");
        diagnosisDetailTvCycle.setText(!StringUtil.isStrEmpty(agrDisease.getCycle()) ? agrDisease.getCycle() : "暂无数据");
        diagnosisDetailTvFactor.setText(!StringUtil.isStrEmpty(agrDisease.getFactor()) ? agrDisease.getFactor() : "暂无数据");
        diagnosisDetailTvMethod.setText(!StringUtil.isStrEmpty(agrDisease.getMethod()) ? agrDisease.getMethod() : "暂无数据");

    }

    /**
     * 设置顶部banner
     *
     * @param disPicList
     */
    private void setBanner(List<PestDetailVO.DisPicListBean> disPicList) {
        if (disPicList != null && disPicList.size() > 0) {
            diagnosisDetailBanner.setVisibility(View.VISIBLE);
            List<String> imagesList = new ArrayList<>();
            for (int i = 0; i < disPicList.size(); i++) {
                imagesList.add(URL.URL_SHOW_IMAGE + StringUtil.removeCharRear(disPicList.get(i).getPicFileName(), ".") + Constant.SHOW_IMAGE_TYPE_SMALL);
            }
            //设置图片加载器
            diagnosisDetailBanner.setImageLoader(new GlideImageLoader(1));
            //设置图片集合
            diagnosisDetailBanner.setImages(imagesList);
            //设置风格
            diagnosisDetailBanner.setBannerStyle(BannerConfig.NUM_INDICATOR);
            //设置是否自动轮播
            diagnosisDetailBanner.isAutoPlay(false);
            //banner设置方法全部调用完毕时最后调用
            diagnosisDetailBanner.start();
        } else
            diagnosisDetailBanner.setVisibility(View.GONE);
    }

    @Override
    public void rightOnClick() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.diagnosis_detail_iv_chat, R.id.diagnosis_detail_layout_expert})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.diagnosis_detail_iv_chat:
                break;
            case R.id.diagnosis_detail_layout_expert:
                GotoUtil.gotoActivity(PestDiagnosisDetailActivity.this, ExpertConsultationActivity.class);
                break;
        }
    }
}
